<template>
  <div>
    <!-- 添加或修改采购计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="68px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="资产类别" prop="assetTypeId">
              <treeselect
                v-model="form.assetTypeId"
                :options="options"
                :normalizer="normalizer"
                placeholder="选择类别"
                @select="
                  (node) => {
                    form.assetTypeName = node.name;
                  }
                "
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入固定资产名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="规格型号" prop="model">
              <el-input v-model="form.model" placeholder="请输入规格型号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="数量" prop="number">
              <el-input
                type="number"
                v-model="form.number"
                controls-position="right"
                :min="0"
                placeholder="请输入数量"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="价格" prop="price">
              <el-input
                type="number"
                v-model="form.price"
                controls-position="right"
                :min="0"
                placeholder="请输入价格"
                ><template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="购买理由" prop="remark">
              <el-input
                type="textarea"
                v-model="form.remark"
                placeholder="请输入购买理由"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('保存')">保存</el-button>
        <el-button type="primary" @click="submitForm('提交')">提交</el-button>
        <el-button @click="() => (this.open = false)">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { get, add, edit } from "@/api/prod/purchase/plan";
import { pageList as option } from "@/api/prod/purchase/asset-type";
import { handleOptions } from "@/utils/base/options";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "AddAssetType",
  dicts: ["sys_normal_disable"],
  components: { Treeselect },
  data() {
    const baseForm = {
      id: null,
      assetTypeId: null,
      assetTypeName: null,
      name: null,
      model: null,
      unit: null,
      number: null,
      price: null,
      remark: null,
      action: null,
    };

    return {
      // 表单类型
      type: null,
      // 部门树选项
      deptOptions: [],
      // 是否显示弹出层
      open: false,
      // 基础表单参数
      baseForm,
      // 表单参数
      form: { ...baseForm },
      // 表单校验
      rules: {},
      // 选项
      options: [],
    };
  },

  created() {
    // 资产类别
    option({}).then((response) => {
      this.options = handleOptions(response.data);
    });
  },

  computed: {
    // 弹出层标题
    title() {
      if (this.type !== null) return this.type + "-采购计划";
      return "";
    },
  },

  methods: {
    // 表单重置
    reset() {
      this.type = null;
      this.form = { ...this.baseForm };
    },

    /** 转换资产类别数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children,
      };
    },

    /** 弹出框打开 */
    show(type, id) {
      this.reset();
      this.open = true;
      this.type = type;

      if (id != null && id != undefined) {
        get(id).then((response) => {
          this.form = response.data;
        });
      }
    },

    /** 提交按钮 */
    submitForm(action) {
      this.form.action = action;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.type == "修改") {
            edit(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit("refresh");
            });
          } else {
            add(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit("refresh");
            });
          }
        }
      });
    },
  },
};
</script>

<style>
</style>